package com.xiangxiao.rpan.file.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.xiangxiao.rpan.file.constant.ResponseCode;
import com.xiangxiao.rpan.file.constant.ResponseMessage;
import com.xiangxiao.rpan.file.consumer.IConsumerStorageClient;
import com.xiangxiao.rpan.file.dto.FileQueryDto;
import com.xiangxiao.rpan.file.dto.JsonObject;
import com.xiangxiao.rpan.file.vo.FileInfoResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2023/12/26 18:51
 */
@Slf4j
@RestController
@RequestMapping("/api/storage")
public class FileController {
  @Resource
  private IConsumerStorageClient consumerStorageClient;

  @PostMapping("/files")
  public JsonObject queryFiles(@RequestBody FileQueryDto fileQueryDto) {
    try {
      JSONObject resp = consumerStorageClient.getFiles(fileQueryDto);
      System.out.println(resp);
      FileInfoResultVo data = resp.getObject("data", new TypeReference<FileInfoResultVo>() {});
      return new JsonObject(ResponseMessage.QUERY_FILE_LIST_SUCCESS, ResponseCode.OK, data);
    } catch (Exception e) {
      e.printStackTrace();
      return new JsonObject(ResponseMessage.QUERY_FILE_LIST_FAILED, ResponseCode.QUERY_FILE_LIST_FAILED);
    }
  }
}
